package com.cloudxingkong.dianyinke.Student.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.bean.StudentDt;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by LeeQ on 2016-5-9.
 */
public class StuGetDTDetailCommAdapter extends BaseAdapter {
    private List<StudentDt.ListBean.DetailBean> data;
    private Context context;

    public StuGetDTDetailCommAdapter(List<StudentDt.ListBean.DetailBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data==null?0: data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommViewHolder holder;
        if (convertView == null) {
            holder=new CommViewHolder();
            convertView = View.inflate(context, R.layout.item_dt_detail_comm, null);
            holder.ivAvatar = (ImageView) convertView.findViewById(R.id.item_dtd_avatar);
            holder.tvContent = (TextView) convertView.findViewById(R.id.item_dtd_content);
            holder.tvDate = (TextView) convertView.findViewById(R.id.item_dtd_date);
            holder.tvName = (TextView) convertView.findViewById(R.id.item_dtd_name);
            convertView.setTag(holder);
        }else {
            holder = (CommViewHolder) convertView.getTag();
        }

        if (data.get(position).getType().equals(holder.TYPE_MSG)) {
            holder.tvName.setText(data.get(position).getMemname());
            holder.tvContent.setText(data.get(position).getMsg().toString());
            String ts = data.get(position).getTs();
            String dateSub = ts.substring(ts.indexOf("-") + 1, ts.indexOf(":") + 3);
            holder.tvDate.setText(dateSub);

            //头像
            int id = data.get(position).getId();
            holder.ivAvatar.setTag(id);
            if (data.get(position).getAvatar() != null && !data.get(position).getAvatar().equals("")) {
                if (holder.ivAvatar.getTag()!=null&&holder.ivAvatar.getTag().equals(id)){
                    String avatarPath = data.get(position).getAvatar().toString();
                    ImageLoader.getInstance().displayImage(avatarPath,holder.ivAvatar);
                }
            }
        }

        return convertView;
    }

    private class CommViewHolder{
        ImageView ivAvatar;
        TextView  tvName,tvContent,tvDate;
        String TYPE_LIKE="like";
        String TYPE_MSG="msg";
    }
}
